//
//  LTHomeworkExamListView.m
//  LearnTeacher
//
//  Created by zyl on 2024/4/12.
//

#import "LTHomeworkExamListView.h"

@implementation LTHomeworkExamListView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.tableView];
        self.backgroundColor = LTWhiteColor;
    }
    return self;
}
#pragma mark -UITableViewDelegate,UITableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@""];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        cell.backgroundColor = LTWhiteColor;
    }
    
    [cell removeAllSubviews];
    NSArray *array = self.data[indexPath.row];
    for (int i = 0; i < array.count; i++) {
        UILabel *titlelab = self.titleLabels[i];
        if (i == 1) {
            UIButton *btn = ({
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.frame = titlelab.frame;
                [button setTitle:[NSString stringWithFormat:@"%@",array[i]] forState:UIControlStateNormal];
                button.titleLabel.font = RFontWithSize(13);
                [button setImage:[UIImage imageNamed:@"ic_homework_arrow_right_small"] forState:UIControlStateNormal];
                [button setTitleColor:rgba(114, 122, 142, 1) forState:UIControlStateNormal];
                button;
            });
            btn.height = 36;
            btn.width =  btn.width + 13;
            [btn layoutButtonWithEdgeInsetsStyle:LButtonEdgeInsetsStyleRight imageTitleSpace:8];
            [cell addSubview:btn];
        }else{
            
            UILabel *contentLab = [[UILabel alloc] initWithFrame:titlelab.frame];
            contentLab.height = 36;
            contentLab.font = RFontWithSize(13);
            contentLab.text = [NSString stringWithFormat:@"%@",array[i]];
            [contentLab setTextAlignment:NSTextAlignmentCenter];
            contentLab.textColor = LTTextBlack;
            [cell addSubview:contentLab];
        }
    }
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.data.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    return 36;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 48;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] init];
    
    headerView.frame = CGRectMake(0,0,LTScreenWidth - 64,48);
    headerView.backgroundColor = LTWhiteColor;
    UIView *bgView = [[UIView alloc] init];
    
    bgView.frame = CGRectMake(0,0,LTScreenWidth - 64,40);
    bgView.backgroundColor = rgba(122, 167, 255, 1);
    [headerView addSubview:bgView];
    [bgView setCorners:UIRectCornerAllCorners cornerRadius:12];
   
    for (int i = 0; i < self.titleLabels.count; i++) {
        UILabel *titlelab = self.titleLabels[i];
        [headerView addSubview:titlelab];
        
    }
    return headerView;
}
- (void)setTitles:(NSArray *)titles
{
    _titles = titles;
    
    self.titleLabels = @[].mutableCopy;
    double x = 0;
    double w = (LTScreenWidth - 64)/2.0;
    for (int i = 0; i < self.titles.count; i++) {
        
        
        UILabel * titleLab = [[UILabel alloc] initWithFrame:CGRectMake(x, 0, w, 40)];
        x = x + w;
        
        titleLab.font = SFontWithSize(14);
        NSString *str = self.titles[i];
        [titleLab setTextAlignment:NSTextAlignmentCenter];
        titleLab.textColor = LTWhiteColor;
        NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:str];
        [att addAttribute:NSFontAttributeName value:SFontWithSize(14) range:str.rangeOfAll];
        
        titleLab.attributedText = att;
        [self.titleLabels addObject:titleLab];
    }
    [self.tableView reloadData];
    
}
- (void)setData:(NSArray *)data
{
    _data = data;
    self.tableView.height = self.data.count*36 + 46;
    if (@available(iOS 15.0, *)) {
        self.tableView.sectionHeaderTopPadding = 0;
    }
    [self.tableView reloadData];
}
-(UITableView *)tableView{
    if (!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(16, 16, LTScreenWidth - 48, 16) style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        _tableView.bounces = NO;
        [_tableView setBackgroundColor:LTClearColor];
        [_tableView setSeparatorColor:LTClearColor];
            
    }
    
    return _tableView;
}
@end
